package com.leyou.page.service.impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.PageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String SPU_KEY_PREFIX = "page:spu:id:";
    private static final String DETAIL_KEY_PREFIX = "page:detail:id:";
    private static final String SKU_KEY_PREFIX = "page:sku:id:";
    private static final String CATEGORY_KEY_PREFIX = "page:category:id:";
    private static final String BRAND_KEY_PREFIX = "page:brand:id:";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";

    @Override
    public String loadSpuData(Long spuId) {
        // 1.查询spu信息
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        if (spuDTO == null || spuDTO.getId() == null) {
            throw new LyException(404, "商品不存在！");
        }
        // 2.转换vo
        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        // 3.序列化为json字符串
        String json = JsonUtils.toJson(spuVO);
        // 4.写入redis
        redisTemplate.opsForValue().set(SPU_KEY_PREFIX + spuId, json);
        // 5.返回
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        // 1.查询spuDetail信息
        SpuDetailDTO detailDTO = itemClient.querySpuDetailById(spuId);
        if (detailDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        // 3.序列化为json字符串
        String json = JsonUtils.toJson(detailDTO);
        // 4.写入redis
        redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX + spuId, json);
        // 5.返回
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        // 1.查询sku信息
        List<SkuDTO> skuDTOList = itemClient.querySkuBySpuId(spuId);
        if (skuDTOList == null || skuDTOList.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        // 3.序列化为json字符串
        String json = JsonUtils.toJson(skuDTOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(SKU_KEY_PREFIX + spuId, json);
        // 5.返回
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        // 1.查询category信息
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        if (categoryDTOList == null || categoryDTOList.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        // 2.转换vo
        List<CategoryVO> categoryVOList = categoryDTOList.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class))
                .collect(Collectors.toList());
        // 3.序列化为json字符串
        String json = JsonUtils.toJson(categoryVOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX + ids.get(2), json);
        // 5.返回
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        // 1.查询category信息
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        if (brandDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        // 2.转换vo
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);

        // 5.返回
        return saveBrand2Redis(brandVO);
    }

    public String saveBrand2Redis(BrandVO brandVO) {
        // 3.序列化为json字符串
        String json = JsonUtils.toJson(brandVO);
        // 4.写入redis
        redisTemplate.opsForValue().set(BRAND_KEY_PREFIX + brandVO.getId(), json);
        return json;
    }

    @Override
    public void deleteBrandById(Long brandId) {
        redisTemplate.delete(BRAND_KEY_PREFIX + brandId);
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 1.根据分类id查询规格组及组内参数
        List<SpecGroupDTO> specGroups = itemClient.querySpecList(categoryId);
        if (specGroups == null || specGroups.size() <= 0) {
            throw new LyException(404, "商品不存在！");
        }
        // 2.转换VO
        List<SpecGroupVO> groupVOList = new ArrayList<>(specGroups.size());
        for (SpecGroupDTO specGroup : specGroups) {
            // 2.1.创建VO
            SpecGroupVO groupVO = new SpecGroupVO();
            // 2.2.放入集合
            groupVOList.add(groupVO);
            // 2.3.填写属性
            // name属性
            groupVO.setName(specGroup.getName());
            // params属性
            List<SpecParamVO> params = specGroup.getParams()
                    .stream()
                    .map(paramDTO -> paramDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            groupVO.setParams(params);
        }
        // 3.序列化为json
        String json = JsonUtils.toJson(groupVOList);
        // 4.写入redis
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX + categoryId, json);
        // 5.返回
        return json;
    }

    @Override
    public void deleteSkuList(Long spuId) {
        redisTemplate.delete(SKU_KEY_PREFIX + spuId);
    }
}
